package com.dz.finance.common.filter;

import com.dz.finance.common.service.AuthorService;
import com.dz.finance.common.entity.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: DengHaoQi
 * @date: 2024/6/10 20:27
 * @description: Token认证过滤器
 */

@Slf4j
@Component
@RequiredArgsConstructor
public class AuthorTokenFilter extends OncePerRequestFilter {

    private final AuthorService<User> authorService;

    @Override
    protected void doFilterInternal(@NotNull HttpServletRequest request,
                                    @NotNull HttpServletResponse response,
                                    @NotNull FilterChain filterChain) throws ServletException, IOException {
        log.info("Token认证过滤器.....");
        authorService.doFilterInternal(request,response,filterChain);
    }


    /*
    * 白名单
    * 返回true不进行拦截
    * */
    @Override
    protected boolean shouldNotFilter(@NotNull HttpServletRequest request) throws ServletException {
        return authorService.shouldNotFilter(request);
    }
}
